package org.lq.ht.view.Supplier;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import org.lq.ht.entity.Supplier;
import org.lq.ht.service.SupplierService;
import org.lq.ht.service.impl.SupplierServiceImpl;
import org.lq.ht.util.CastUtil;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;

public class SupplierAdd extends JFrame {

	private JPanel contentPane;
	private JTextField txt_no;
	private JTextField txt_name;
	private JTextField txt_phone;
	private JTextField txt_card;
	private JTextField txt_address;
	private JTextField txt_companyname;
	private JTextPane text_desc;
	private JButton btnExit;
	private JButton btnSave;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					SupplierAdd frame = new SupplierAdd();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	public SupplierAdd(int no,boolean bool) {
		this();
		Supplier s = new SupplierServiceImpl().getById(no);
		txt_no.setText(CastUtil.castString(s.getNo()));
		txt_name.setText(s.getName());
		txt_phone.setText(s.getPhone());
		txt_card.setText(CastUtil.castString(s.getCard_no()));
		txt_address.setText(s.getAddress());
		txt_companyname.setText(s.getCompany_name());
		text_desc.setText(s.getNote());
		
		if (bool == false) {
			//设置控件是否可用
			btnSave.setEnabled(false);
			//获取当前窗体的所有组件,获取当前Pane中的组件个数
			int count = contentPane.getComponentCount();
			for (int i = 0; i < count; i++) {
				//通过下标获取panel中的某一个组件
				Component component = contentPane.getComponent(i);
				//如果当前组件是JTextFiled就把它改为不可编辑
				if (component instanceof JTextField) {
					JTextField j = (JTextField) component;
					j.setEnabled(false);
				}else if (component instanceof JTextPane) {
					//如果是多行文本,也将其改为不可编辑
					JTextPane t = (JTextPane) component;
					t.setEnabled(false);
				}
			}
		}
	}
	
	/**
	 * Create the frame.
	 */
	public SupplierAdd() {
		setTitle("供应商添加");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 368, 561);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("编号：");
		lblNewLabel.setBounds(58, 30, 58, 15);
		contentPane.add(lblNewLabel);
		
		txt_no = new JTextField();
		txt_no.setEnabled(false);
		txt_no.setBounds(134, 27, 109, 21);
		contentPane.add(txt_no);
		txt_no.setColumns(10);
		
		JLabel lblNewLabel_1 = new JLabel("姓名：");
		lblNewLabel_1.setBounds(58, 65, 58, 15);
		contentPane.add(lblNewLabel_1);
		
		txt_name = new JTextField();
		txt_name.setColumns(10);
		txt_name.setBounds(134, 62, 109, 21);
		contentPane.add(txt_name);
		
		JLabel lblNewLabel_2 = new JLabel("手机号：");
		lblNewLabel_2.setBounds(58, 105, 58, 15);
		contentPane.add(lblNewLabel_2);
		
		txt_phone = new JTextField();
		txt_phone.setColumns(10);
		txt_phone.setBounds(134, 102, 109, 21);
		contentPane.add(txt_phone);
		
		JLabel lblNewLabel_3 = new JLabel("银行卡号：");
		lblNewLabel_3.setBounds(58, 147, 77, 15);
		contentPane.add(lblNewLabel_3);
		
		txt_card = new JTextField();
		txt_card.setColumns(10);
		txt_card.setBounds(134, 144, 109, 21);
		contentPane.add(txt_card);
		
		JLabel lblNewLabel_4 = new JLabel("地址：");
		lblNewLabel_4.setBounds(58, 189, 58, 15);
		contentPane.add(lblNewLabel_4);
		
		txt_address = new JTextField();
		txt_address.setColumns(10);
		txt_address.setBounds(134, 186, 109, 21);
		contentPane.add(txt_address);
		
		JLabel lblNewLabel_5 = new JLabel("公司名称：");
		lblNewLabel_5.setBounds(58, 233, 77, 15);
		contentPane.add(lblNewLabel_5);
		
		txt_companyname = new JTextField();
		txt_companyname.setColumns(10);
		txt_companyname.setBounds(134, 230, 109, 21);
		contentPane.add(txt_companyname);
		
		JLabel lblNewLabel_6 = new JLabel("备注：");
		lblNewLabel_6.setBounds(58, 278, 58, 15);
		contentPane.add(lblNewLabel_6);
		
		text_desc = new JTextPane();
		text_desc.setBounds(134, 278, 132, 79);
		contentPane.add(text_desc);
		  
		btnSave = new JButton("保存");
		btnSave.setForeground(Color.WHITE);
		btnSave.setBackground(Color.GREEN);
		btnSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Supplier s = new Supplier();
				SupplierService ss = new SupplierServiceImpl();
//				if (!isNull()) {
//					JOptionPane.showMessageDialog(SupplierAdd.this, "信息不能为空!");
//				}else
				if (!ss.isPhone(txt_phone.getText())) {
					JOptionPane.showMessageDialog(SupplierAdd.this, "手机号必须是1开头的11位数!");
				}
				else {
					s.setName(txt_name.getText());
					s.setPhone(txt_phone.getText());
					s.setAddress(txt_address.getText());
					s.setCompany_name(txt_companyname.getText());
					s.setCard_no(CastUtil.castInt(txt_card.getText()));
					s.setNote(text_desc.getText());
				}
				SupplierService supplierService = new SupplierServiceImpl();
				int no = CastUtil.castInt(txt_no.getText());
				if(no>0) {
					s.setNo(no);
					if(isNull()&&ss.isPhone(txt_phone.getText())&&supplierService.updateSupplier(s)) {
						JOptionPane.showMessageDialog(SupplierAdd.this, "供货商修改成功!");
						dispose();
					}else {
						if (!isNull()) {
							JOptionPane.showMessageDialog(SupplierAdd.this, "信息不能为空!");
						}
						JOptionPane.showMessageDialog(SupplierAdd.this, "供货商修改失败!");
					}
				}else {
					if(isNull()&&ss.isPhone(txt_phone.getText())&&supplierService.saveSupplier(s)) {
						JOptionPane.showMessageDialog(SupplierAdd.this, "供货商添加成功!");
						int num = JOptionPane.showConfirmDialog(SupplierAdd.this, "是否继续添加?");
						if (num == 0) {
							txt_address.setText("");
							txt_card.setText("");
							txt_companyname.setText("");
							txt_name.setText("");
							txt_no.setText("");
							txt_phone.setText("");
							text_desc.setText("");
						}else {
							dispose();
						}
					}else {
						if (!isNull()) {
							JOptionPane.showMessageDialog(SupplierAdd.this, "信息不能为空!");
						}
						JOptionPane.showMessageDialog(SupplierAdd.this, "供货商添加失败!");
					}
				}
			}
		});
		btnSave.setBounds(58, 401, 97, 23);
		contentPane.add(btnSave);
		
		btnExit = new JButton("取消");
		btnExit.setForeground(Color.WHITE);
		btnExit.setBackground(Color.GREEN);
		btnExit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
		btnExit.setBounds(190, 401, 97, 23);
		contentPane.add(btnExit);
	}
	/**
	 * 判断文本框是否为空
	 * @return
	 */
	public boolean isNull() {
		boolean flag = !txt_address.getText().equals("")&&!txt_card.getText().equals("")&&!txt_companyname.getText().equals("")&&
				!txt_name.getText().equals("")&&!txt_phone.getText().equals("")&&!text_desc.getText().equals("");
		return flag;
	}
	
}
